package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(path = "/member")
@CrossOrigin
@Slf4j
public class MemberController {
    @Reference
    private MemberService memberService;

    /**
     * 分页查询会员
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            log.info("queryPageBean={}", queryPageBean);

            if (queryPageBean.getQueryString()!=null) {
                //有判断条件时
                PageResult nameorPhoneorfileNumber = memberService.findNameOrPhoneOrFileNumber(queryPageBean);
                System.out.println(nameorPhoneorfileNumber);
                return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, nameorPhoneorfileNumber);
            }
           //当没有判断条件是就查询全部
            PageResult pageResult = memberService.findAll(queryPageBean);
            log.info("pageResult={}", pageResult);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

        /**
         * 根据id打开编辑信息
         *
         * @param id
         * @return
         */
        @GetMapping(path = "/{id}")
        public Result findById(@PathVariable int id) {
            try {
                //点击编辑时获取该条数据的ID
                log.info("id={}",id);
                Member member = memberService.findById(id);
                return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, member);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
            }
        }

        /**
         *修改信息
         *
         * @param
         * @param
         * @return
         */
        @PutMapping(path = "/update")
        public Result edit(@RequestBody Member member) {
            try {
                log.info("member={}",member);
                memberService.update(member);
                return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
            }
        }


    /**
     * 新增
     * @param member
     * @return
     */
    @PostMapping(path = "/add")
    public Result add(@RequestBody Member member) {
        try {
            member.setStatus(1);
            log.info("member={}",member);
            //通过时间戳获取当前时间将数据转换为date格式
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long l = System.currentTimeMillis();
            String format1 = format.format(l);
            Date date = format.parse(format1);
            member.setRegTime(date);

            Boolean result = memberService.save(member);
            if (result){
                return new Result(result, MessageConstant.ADD_CHECKITEM_SUCCESS);
            }
            return new Result(result, MessageConstant.ADD_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }





    //状态权限设置
    @PutMapping("/{userid}/{status}")
    public Result updatastatus(@PathVariable ("userid") String userid,@PathVariable("status") Integer status) {
        try {
            //当前数据的ID
            log.info("userid={}",userid);
            //当前数据的装态 前端传过来的的状态有两种形式开启为(1)禁用为(0)
            memberService.updataStatus(userid,status);
            return new Result(true, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }
    }

}
